package com.cennavi.mineplay.data;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cennavi.mineplay.utils.DBUtils;

@RestController
@RequestMapping("/audipoc")
public class AudipocCluster {
	@Autowired
	protected JdbcTemplate jdbc;
	
	@RequestMapping(value = "/cluster")
	public Object geojson(int zoom,int hour,int cnum) {
		
		
		String sql = "with a1 as ( select ','||string_agg(cast(charging_id as text),',')||',' cids,cid,st_asgeojson(st_centroid(st_union(geom))) as geojson from ( select  ST_ClusterKMeans(b.geom,"+cnum+") over() as cid,b.charging_id,geom from  audi_charging_location as b ) as foo group by cid ) select a1.geojson,sum(cnt) as cnt from audi_charging_times_data as b,a1 where b.hour="+hour+" and position(','||b.charging_id||',' in a1.cids)>=1 group by cids,a1.geojson";
		
		 

		try {
			Object value = DBUtils.getGeojson(jdbc, sql);
			
			return value;

		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
		
	}
	
}
